package com.yuyou.fn.portal.service.impl;

import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.BigDecimals;
import com.yuyou.fn.educational.entity.CourseOrder;
import com.yuyou.fn.educational.entity.DiscountCode;
import com.yuyou.fn.educational.entity.Student;
import com.yuyou.fn.educational.entity.StudentReg;
import com.yuyou.fn.educational.service.*;
import com.yuyou.fn.educational.vo.EmployeeDiscountVo;
import com.yuyou.fn.educational.vo.ThirdpartyDiscountCodeVo;
import com.yuyou.fn.portal.service.IDiscountQueryService;
import com.yuyou.fn.portal.vo.QueryDiscountCodeResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class DiscountQueryServiceImpl implements IDiscountQueryService {

    @Resource
    private ICourseOrderService courseOrderService;

    @Resource
    private IStudentRegService studentRegService;

    @Resource
    private IEmployeeDiscountService employeeDiscountService;

    @Resource
    private IThirdpartyDiscountCodeService thirdpartyDiscountCodeService;

    @Resource
    private IDiscountCodeService discountCodeService;

    @Resource
    private ICourseClassService courseClassService;

    @Resource
    private IStudentService studentService;



    @Override
    public QueryDiscountCodeResult queryDiscountCodeResult(Long outerOrderId, String discountCode, Long discountId) {

        if( !discountId.equals(1l) && !discountId.equals(2l) &&  !discountId.equals(3l)){
            throw new BusException("Invalid discountId");
        }

        CourseOrder courseOrder = courseOrderService.findCourseOrderByParentOrderId(outerOrderId).get(0);

        if(discountId.equals(1l)){
            List<StudentReg> studentRegs = studentRegService.findStudentRegByCourseOrderId(courseOrder.getCourseOrderId());
            List<Long> classIds = BeanUtils.toList(studentRegs, "classId");
            DiscountCode discountCodeObj = discountCodeService.checkCode(courseOrder.getCourseOrderId(),discountCode, courseOrder.getPayableAmount(), courseClassService.findByIds(classIds));
            QueryDiscountCodeResult queryDiscountCodeResult = new QueryDiscountCodeResult();
            queryDiscountCodeResult.setDiscountAmount(discountCodeObj.getDiscountAmount());
            queryDiscountCodeResult.setDiscountCode(discountCode);
            queryDiscountCodeResult.setEffectiveEndTime(discountCodeObj.getEffectiveEndTime());
            queryDiscountCodeResult.setEffectiveStartTime(discountCodeObj.getEffectiveStartTime());
            queryDiscountCodeResult.setName(discountCodeObj.getName() + ":"  + courseOrder.getStudentName() );
            queryDiscountCodeResult.setDiscountType(1);
            return queryDiscountCodeResult;
        }else if(discountId.equals(2l)){

            Student student = studentService.findById(courseOrder.getStudentId());
            EmployeeDiscountVo employeeDiscountVo = employeeDiscountService.checkCode(courseOrder.getCourseOrderId(),discountCode, student.getName(), student.getStudentNo());
            QueryDiscountCodeResult queryDiscountCodeResult = new QueryDiscountCodeResult();
            queryDiscountCodeResult.setDiscountAmount(employeeDiscountVo.getDiscountValue());
            queryDiscountCodeResult.setDiscountCode(discountCode);
            queryDiscountCodeResult.setEffectiveEndTime(employeeDiscountVo.getEndDate());
            queryDiscountCodeResult.setEffectiveStartTime(employeeDiscountVo.getBeginDate());
            queryDiscountCodeResult.setName( employeeDiscountVo.getMainCategoryName()+ ":" + student.getName() );
            queryDiscountCodeResult.setDiscountType(0);
            return queryDiscountCodeResult;

        }else if(discountId.equals(3l)){

            Student student = studentService.findById(courseOrder.getStudentId());
            ThirdpartyDiscountCodeVo thirdpartyDiscountCodeVo = thirdpartyDiscountCodeService.checkCode(courseOrder.getCourseOrderId(), courseOrder.getPayableAmount(), discountCode, student.getStudentId(), student.getName(), student.getStudentNo());
            QueryDiscountCodeResult queryDiscountCodeResult = new QueryDiscountCodeResult();
            queryDiscountCodeResult.setDiscountAmount(thirdpartyDiscountCodeVo.getDiscountType() == 0 || thirdpartyDiscountCodeVo.getDiscountType() == 1 ? BigDecimals.integer(thirdpartyDiscountCodeVo.getDiscountAmount()): thirdpartyDiscountCodeVo.getDiscountAmount() );
            queryDiscountCodeResult.setDiscountCode(discountCode);
            queryDiscountCodeResult.setEffectiveEndTime(thirdpartyDiscountCodeVo.getEndDate());
            queryDiscountCodeResult.setEffectiveStartTime(thirdpartyDiscountCodeVo.getBeginDate());
            queryDiscountCodeResult.setName( thirdpartyDiscountCodeVo.getTypeName() );
            queryDiscountCodeResult.setDiscountType(thirdpartyDiscountCodeVo.getDiscountType() == 0 || thirdpartyDiscountCodeVo.getDiscountType() == 1 ? 1 : 0 );
            return queryDiscountCodeResult;

        }else{
            throw new IllegalArgumentException("Unknown discountId");
        }
    }
}
